package com.guofen.service.shlj.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.ShljAbnormalGarbageMapper;
import com.guofen.pojo.ShljAbnormalGarbage;
import com.guofen.service.shlj.AbnormalGarbageService;
import com.guofen.util.DateAndTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AbnormalGarbageServiceImpl implements AbnormalGarbageService {

    @Autowired
    private ShljAbnormalGarbageMapper shljAbnormalGarbageMapper;

    @Override
    public boolean addForm(String wagon_number, String garbage_source, String garbage_type, Double garbage_number, String recording_person, String remarks) {
        //获取当前日期
        String date = DateAndTimeUtil.getDate();

        ShljAbnormalGarbage shljAbnormalGarbage = new ShljAbnormalGarbage();
        shljAbnormalGarbage.setDate(date);
        shljAbnormalGarbage.setWagonNumber(wagon_number);
        shljAbnormalGarbage.setGarbageSource(garbage_source);
        shljAbnormalGarbage.setGarbageType(garbage_type);
        shljAbnormalGarbage.setGarbageNumber(garbage_number);
//        shljAbnormalGarbage.setGarbageUnit(garbage_unit);
        shljAbnormalGarbage.setRecordingPerson(recording_person);
        shljAbnormalGarbage.setRemarks(remarks);
        int result = shljAbnormalGarbageMapper.insert(shljAbnormalGarbage);
        if (result == 1)
            return true;
        else
            return false;
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<ShljAbnormalGarbage> list = shljAbnormalGarbageMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<ShljAbnormalGarbage> list = shljAbnormalGarbageMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return shljAbnormalGarbageMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(ShljAbnormalGarbage record) {
        return shljAbnormalGarbageMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public boolean addFormExcel(String date, String wagon_number, String garbage_source, String garbage_type, Double garbage_number,
                         String garbage_unit, String recording_person, String remarks){

        ShljAbnormalGarbage shljAbnormalGarbage = new ShljAbnormalGarbage();
        shljAbnormalGarbage.setDate(date);
        shljAbnormalGarbage.setWagonNumber(wagon_number);
        shljAbnormalGarbage.setGarbageSource(garbage_source);
        shljAbnormalGarbage.setGarbageType(garbage_type);
        shljAbnormalGarbage.setGarbageNumber(garbage_number);
        shljAbnormalGarbage.setGarbageUnit(garbage_unit);
        shljAbnormalGarbage.setRecordingPerson(recording_person);
        shljAbnormalGarbage.setRemarks(remarks);
        int result = shljAbnormalGarbageMapper.insert(shljAbnormalGarbage);
        if (result == 1)
            return true;
        else
            return false;
    }

    @Override
    public ShljAbnormalGarbage findForm() {
        //获取当前日期
        String date = DateAndTimeUtil.getDate();
        System.out.println("当前日期" + date);
        //获取最新记录
        ShljAbnormalGarbage shljAbnormalGarbage = shljAbnormalGarbageMapper.findLast();
        System.out.println("最新记录" + shljAbnormalGarbageMapper.findLast());
        //获取最新记录日期
        String dateLast =  shljAbnormalGarbage.getDate();
        System.out.println("最新记录日期" + dateLast);

        if (date.equals(dateLast))
            return shljAbnormalGarbage;
        else
            return null;
    }
}
